﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace PEC.Forms
{
    public partial class frmSchedule : Form
    {
        PecController _pecController;
        private GroupBox[] gb = new GroupBox[20];
        private TextBox[] v_ThoiGianLam = new TextBox[20];
        private TextBox[] v_ThoiGianNghi = new TextBox[20];
        private TextBox[] v_GioKetThuc = new TextBox[20];

        private int v_IDTo = 0;
        private string v_NgayHienTai = "";
        private string v_TimeDelay = "0";
        //private string v_GioHienTai = "";
        // bien nay luu gio hien tai
        int v_gioBatDau = 0;
        int v_phutBatDau = 0;
        // trang thai form
        int v_TrangThai = 0;
        int _soBang = 1;
        public frmSchedule(int _line,PecController pec)
        {
            _pecController = pec; 
            InitializeComponent();
            v_IDTo = _line;
        }        

        private void ADDGroupBoxConTrol()
        {
            gb[0] = gbC1Time1;
            gb[1] = gbC1Time2;
            gb[2] = gbC1Time3;
            gb[3] = gbC1Time4;
            gb[4] = gbC1Time5;
            gb[5] = gbC1Time6;
            gb[6] = gbC1Time7;
            gb[7] = gbC1Time8;
            gb[8] = gbC1Time9;
            gb[9] = gbC1Time10;
            gb[10] = gbC1Time11;
            gb[11] = gbC1Time12;
            gb[12] = gbC1Time13;
            gb[13] = gbC1Time14;
            gb[14] = gbC1Time15;
            gb[15] = gbC1Time16;
            gb[16] = gbC1Time17;
            gb[17] = gbC1Time18;
            gb[18] = gbC1Time19;
            gb[19] = gbC1Time20;
        }
        private void ADDThoiGianLamConTrol()
        {
            v_ThoiGianLam[0] = txtGioLamThu1;
            v_ThoiGianLam[1] = txtGioLamThu2;
            v_ThoiGianLam[2] = txtGioLamThu3;
            v_ThoiGianLam[3] = txtGioLamThu4;
            v_ThoiGianLam[4] = txtGioLamThu5;
            v_ThoiGianLam[5] = txtGioLamThu6;
            v_ThoiGianLam[6] = txtGioLamThu7;
            v_ThoiGianLam[7] = txtGioLamThu8;
            v_ThoiGianLam[8] = txtGioLamThu9;
            v_ThoiGianLam[9] = txtGioLamThu10;
            v_ThoiGianLam[10] = txtGioLamThu11;
            v_ThoiGianLam[11] = txtGioLamThu12;
            v_ThoiGianLam[12] = txtGioLamThu13;
            v_ThoiGianLam[13] = txtGioLamThu14;
            v_ThoiGianLam[14] = txtGioLamThu15;
            v_ThoiGianLam[15] = txtGioLamThu16;
            v_ThoiGianLam[16] = txtGioLamThu17;
            v_ThoiGianLam[17] = txtGioLamThu18;
            v_ThoiGianLam[18] = txtGioLamThu19;
            v_ThoiGianLam[19] = txtGioLamThu20;
        }
        private void ADDThoiGianNghiConTrol()
        {
            v_ThoiGianNghi[0] = txtGioNghiThu1;
            v_ThoiGianNghi[1] = txtGioNghiThu2;
            v_ThoiGianNghi[2] = txtGioNghiThu3;
            v_ThoiGianNghi[3] = txtGioNghiThu4;
            v_ThoiGianNghi[4] = txtGioNghiThu5;
            v_ThoiGianNghi[5] = txtGioNghiThu6;
            v_ThoiGianNghi[6] = txtGioNghiThu7;
            v_ThoiGianNghi[7] = txtGioNghiThu8;
            v_ThoiGianNghi[8] = txtGioNghiThu9;
            v_ThoiGianNghi[9] = txtGioNghiThu10;
            v_ThoiGianNghi[10] = txtGioNghiThu11;
            v_ThoiGianNghi[11] = txtGioNghiThu12;
            v_ThoiGianNghi[12] = txtGioNghiThu13;
            v_ThoiGianNghi[13] = txtGioNghiThu14;
            v_ThoiGianNghi[14] = txtGioNghiThu15;
            v_ThoiGianNghi[15] = txtGioNghiThu16;
            v_ThoiGianNghi[16] = txtGioNghiThu17;
            v_ThoiGianNghi[17] = txtGioNghiThu18;
            v_ThoiGianNghi[18] = txtGioNghiThu19;
            v_ThoiGianNghi[19] = txtGioNghiThu20;
        }
        private void ADDGioKetThucConTrol()
        {
            v_GioKetThuc[0] = txtGioKetThucC1;
            v_GioKetThuc[1] = txtGioKetThucC2;
            v_GioKetThuc[2] = txtGioKetThucC3;
            v_GioKetThuc[3] = txtGioKetThucC4;
            v_GioKetThuc[4] = txtGioKetThucC5;
            v_GioKetThuc[5] = txtGioKetThucC6;
            v_GioKetThuc[6] = txtGioKetThucC7;
            v_GioKetThuc[7] = txtGioKetThucC8;
            v_GioKetThuc[8] = txtGioKetThucC9;
            v_GioKetThuc[9] = txtGioKetThucC10;
            v_GioKetThuc[10] = txtGioKetThucC11;
            v_GioKetThuc[11] = txtGioKetThucC12;
            v_GioKetThuc[12] = txtGioKetThucC13;
            v_GioKetThuc[13] = txtGioKetThucC14;
            v_GioKetThuc[14] = txtGioKetThucC15;
            v_GioKetThuc[15] = txtGioKetThucC16;
            v_GioKetThuc[16] = txtGioKetThucC17;
            v_GioKetThuc[17] = txtGioKetThucC18;
            v_GioKetThuc[18] = txtGioKetThucC19;
            v_GioKetThuc[19] = txtGioKetThucC20;
        }

        private void FormTime_Load(object sender, EventArgs e)
        {
           // _soBang = Convert.ToInt32(Classes.frmMain.__config.ScadaSetting.Rows[0][Classes.frmMain.__config.ScadaSetting.SoBangColumn]);
            string to = v_IDTo == 0 ? " All " : v_IDTo.ToString();
            label7.Text = label7.Text + " " + to;
            // vi khi ngay hien tai mat focus se tinhlai thoi gian cho cac textbox
            this.txtNgayHienTai.Text = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();
            this.txtNgayHienTai.Focus();

            // add con trol vao mang cho de su dung
            this.ADDGroupBoxConTrol();
            this.ADDThoiGianLamConTrol();
            this.ADDThoiGianNghiConTrol();
            this.ADDGioKetThucConTrol();
            //

            //if (Classes.frmMain.__config.Timer.Rows.Count < 1)
            //{
            //    InitTimer();
            //}
            //this.setTime(Classes.frmMain.__config.Timer.Rows[0]["Data"].ToString().Split('\n'));
            v_TrangThai = 1;


            if (v_IDTo != 0)
            {
                LoadbyLine(v_IDTo);
                cbSoGioLam.SelectedValue = LoadStageCount(v_IDTo);
            }
            else
            {
                LoadbyLine(v_IDTo);
            }
        }

        private object LoadStageCount(int v_IDTo)
        {
            string sErr = "";
            DTO.ChuyenSanXuatInfo info = new DTO.ChuyenSanXuatInfo();
            BUS.ChuyenSanXuatControl ctr = new BUS.ChuyenSanXuatControl(_pecController.Bus);
            DTO.FilterParameterCollection filters = new DTO.FilterParameterCollection();
            filters.Add("LineId", v_IDTo);
            DataTable dt = ctr.GetByFilterToDataTable(_pecController.XuongId, filters, ref sErr);
            if (dt.Rows.Count == 1)
            {
                info = new DTO.ChuyenSanXuatInfo(dt.Rows[0]);                
            }
            return info.SoGiaiDoan;
        }
        

        private void LoadbyLine(int v_IDTo)
        {
            string sErr = "";
            BUS.OpGioControl ctr = new BUS.OpGioControl(_pecController.Bus);
            DTO.FilterParameterCollection filters = new DTO.FilterParameterCollection();
            
            filters.Add("LineId", v_IDTo);
            //filters.Add("ComId", txtComId.Text);
            DataTable dt = ctr.GetByFilterToDataTable(_pecController.XuongId, (v_IDTo==0? null:filters), ref sErr);
            if (dt.Rows.Count == 1)
            {
                DTO.OpGioInfo info = new DTO.OpGioInfo(dt.Rows[0]);
                SetValue(info);
            }
        }

        private void SetValue(DTO.OpGioInfo info)
        {
            cbSoGioLam.Text = info.SoGiaiDoan.ToString();
            cbSoGioLam.SelectedValue = info.SoGiaiDoan.ToString();
            int giobatdau = int.Parse(info.BatDau.ToString()) / 60;
            int phutbatdau = int.Parse(info.BatDau.ToString()) % 60;
            mtxtGioBatDau.Text = giobatdau.ToString("00") + "" + phutbatdau.ToString("00");
            txtGioLamThu1.Text = info.GioLam1.ToString();
            txtGioNghiThu1.Text = info.GioNghi1.ToString();
            txtGioLamThu2.Text = info.GioLam2.ToString();
            txtGioNghiThu2.Text = info.GioNghi2.ToString();
            txtGioLamThu3.Text = info.GioLam3.ToString();
            txtGioNghiThu3.Text = info.GioNghi3.ToString();
            txtGioLamThu4.Text = info.GioLam4.ToString();
            txtGioNghiThu4.Text = info.GioNghi4.ToString();
            txtGioLamThu5.Text = info.GioLam5.ToString();
            txtGioNghiThu5.Text = info.GioNghi5.ToString();
            txtGioLamThu6.Text = info.GioLam6.ToString();
            txtGioNghiThu6.Text = info.GioNghi6.ToString();
            txtGioLamThu7.Text = info.GioLam7.ToString();
            txtGioNghiThu7.Text = info.GioNghi7.ToString();
            txtGioLamThu8.Text = info.GioLam8.ToString();
            txtGioNghiThu8.Text = info.GioNghi8.ToString();
            txtGioLamThu9.Text = info.GioLam9.ToString();
            txtGioNghiThu9.Text = info.GioNghi9.ToString();
            txtGioLamThu10.Text = info.GioLam10.ToString();
            txtGioNghiThu10.Text = info.GioNghi10.ToString();
            txtGioLamThu11.Text = info.GioLam11.ToString();
            txtGioNghiThu11.Text = info.GioNghi11.ToString();
            txtGioLamThu12.Text = info.GioLam12.ToString();
            txtGioNghiThu12.Text = info.GioNghi12.ToString();
            txtGioLamThu13.Text = info.GioLam13.ToString();
            txtGioNghiThu13.Text = info.GioNghi13.ToString();
            txtGioLamThu14.Text = info.GioLam14.ToString();
            txtGioNghiThu14.Text = info.GioNghi14.ToString();
            txtGioLamThu15.Text = info.GioLam15.ToString();
            txtGioNghiThu15.Text = info.GioNghi15.ToString();
            txtGioLamThu16.Text = info.GioLam16.ToString();
            txtGioNghiThu16.Text = info.GioNghi16.ToString();
            txtGioLamThu17.Text = info.GioLam17.ToString();
            txtGioNghiThu17.Text = info.GioNghi17.ToString();
            txtGioLamThu18.Text = info.GioLam18.ToString();
            txtGioNghiThu18.Text = info.GioNghi18.ToString();
            txtGioLamThu19.Text = info.GioLam19.ToString();
            txtGioNghiThu19.Text = info.GioNghi19.ToString();
            txtGioLamThu20.Text = info.GioLam20.ToString();
            txtGioNghiThu20.Text = info.GioNghi20.ToString();
        }

        private DTO.OpGioInfo GetValue()
        {
            string _tam = this.mtxtGioBatDau.Text;
            string[] _GioPhut = _tam.Split(':');            
            int gioBatDau = Convert.ToInt32(_GioPhut[0]);
            int phutBatDau = Convert.ToInt32(_GioPhut[1]);
            int sophutbatdau = gioBatDau * 60 + phutBatDau;
            DTO.OpGioInfo info = new DTO.OpGioInfo(_pecController.XuongId, v_IDTo, sophutbatdau,
                txtGioLamThu1.Text == "" ? 0 : int.Parse(txtGioLamThu1.Text),
                txtGioLamThu2.Text == "" ? 0 : int.Parse(txtGioLamThu2.Text),
                txtGioLamThu3.Text == "" ? 0 : int.Parse(txtGioLamThu3.Text),
                txtGioLamThu4.Text == "" ? 0 : int.Parse(txtGioLamThu4.Text),
                txtGioLamThu5.Text == "" ? 0 : int.Parse(txtGioLamThu5.Text),
                txtGioLamThu6.Text == "" ? 0 : int.Parse(txtGioLamThu6.Text),
                txtGioLamThu7.Text == "" ? 0 : int.Parse(txtGioLamThu7.Text),
                txtGioLamThu8.Text == "" ? 0 : int.Parse(txtGioLamThu8.Text),
                txtGioLamThu9.Text == "" ? 0 : int.Parse(txtGioLamThu9.Text),
                txtGioLamThu10.Text == "" ? 0 : int.Parse(txtGioLamThu10.Text),
                txtGioLamThu11.Text == "" ? 0 : int.Parse(txtGioLamThu11.Text),
                txtGioLamThu12.Text == "" ? 0 : int.Parse(txtGioLamThu12.Text),
                txtGioLamThu13.Text == "" ? 0 : int.Parse(txtGioLamThu13.Text),
                txtGioLamThu14.Text == "" ? 0 : int.Parse(txtGioLamThu14.Text),
                txtGioLamThu15.Text == "" ? 0 : int.Parse(txtGioLamThu15.Text),
                txtGioLamThu16.Text == "" ? 0 : int.Parse(txtGioLamThu16.Text),
                txtGioLamThu17.Text == "" ? 0 : int.Parse(txtGioLamThu17.Text),
                txtGioLamThu18.Text == "" ? 0 : int.Parse(txtGioLamThu18.Text),
                txtGioLamThu19.Text == "" ? 0 : int.Parse(txtGioLamThu19.Text),
                txtGioLamThu20.Text == "" ? 0 : int.Parse(txtGioLamThu20.Text),
                txtGioNghiThu1.Text == "" ? 0 : int.Parse(txtGioNghiThu1.Text),
                txtGioNghiThu2.Text == "" ? 0 : int.Parse(txtGioNghiThu2.Text),
                txtGioNghiThu3.Text == "" ? 0 : int.Parse(txtGioNghiThu3.Text),
                txtGioNghiThu4.Text == "" ? 0 : int.Parse(txtGioNghiThu4.Text),
                txtGioNghiThu5.Text == "" ? 0 : int.Parse(txtGioNghiThu5.Text),
                txtGioNghiThu6.Text == "" ? 0 : int.Parse(txtGioNghiThu6.Text),
                txtGioNghiThu7.Text == "" ? 0 : int.Parse(txtGioNghiThu7.Text),
                txtGioNghiThu8.Text == "" ? 0 : int.Parse(txtGioNghiThu8.Text),
                txtGioNghiThu9.Text == "" ? 0 : int.Parse(txtGioNghiThu9.Text),
                txtGioNghiThu10.Text == "" ? 0 : int.Parse(txtGioNghiThu10.Text),
                txtGioNghiThu11.Text == "" ? 0 : int.Parse(txtGioNghiThu11.Text),
                txtGioNghiThu12.Text == "" ? 0 : int.Parse(txtGioNghiThu12.Text),
                txtGioNghiThu13.Text == "" ? 0 : int.Parse(txtGioNghiThu13.Text),
                txtGioNghiThu14.Text == "" ? 0 : int.Parse(txtGioNghiThu14.Text),
                txtGioNghiThu15.Text == "" ? 0 : int.Parse(txtGioNghiThu15.Text),
                txtGioNghiThu16.Text == "" ? 0 : int.Parse(txtGioNghiThu16.Text),
                txtGioNghiThu17.Text == "" ? 0 : int.Parse(txtGioNghiThu17.Text),
                txtGioNghiThu18.Text == "" ? 0 : int.Parse(txtGioNghiThu18.Text),
                txtGioNghiThu19.Text == "" ? 0 : int.Parse(txtGioNghiThu19.Text),
                txtGioNghiThu20.Text == "" ? 0 : int.Parse(txtGioNghiThu20.Text),
                int.Parse(cbSoGioLam.Text));
            
                return info;

        }


        private static void InitTimer()
        {
            string tmp = "14;07:30;0;60;0;08:30:00;60;0;09:30:00;60;0;10:30:00;60;0;11:30:00;15;45;12:30:00;60;0;13:30:00;50;10;14:30:00;60;0;15:30:00;60;0;16:30:00;60;0;17:30:00;30;30;18:30:00;0;60;19:30:00;0;60;20:30:00;0;60;21:30:00;0;0;00:00:00;0;0";
            //Classes.frmMain.__config.Timer.AddTimerRow("-1", string.Join("\n", tmp.Split(';')));
        }
        // doc file text va ganlai thong tin sua ngay 06/06/2008
        private void setTime(string[] _thoigian)
        {
            cbSoGioLam.Text = _thoigian[0] == "" ? "0" : _thoigian[0];
            mtxtGioBatDau.Text = _thoigian[1] == "" ? "0" : _thoigian[1];
            txtTGDelay.Text = _thoigian[2] == "" ? "0" : _thoigian[2];

            for (int i = 0; i < 20; i++)
            {
                try
                {
                    v_ThoiGianLam[i].Text = _thoigian[(i * 3) + 3] == "" ? "0" : _thoigian[(i * 3) + 3];
                    v_ThoiGianNghi[i].Text = _thoigian[(i * 3) + 4] == "" ? "0" : _thoigian[(i * 3) + 4];
                    v_GioKetThuc[i].Text = _thoigian[(i * 3) + 5] == "" ? "0" : _thoigian[(i * 3) + 5];
                }
                catch
                {
                    v_ThoiGianLam[i].Text = "0";
                    v_ThoiGianNghi[i].Text = "0";
                    v_GioKetThuc[i].Text = "00:00:00";
                }

            }

        }
        private int GetTimeOfMonth()
        {
            int kq = 0;
            foreach (TextBox x in v_ThoiGianLam)
                if (Regex.IsMatch(x.Text, @"^[0-9]+$"))
                    kq += int.Parse(x.Text);
            return kq;
        }
        // lay du lieu tu control sua ngay 06/06/2008
        private string[] GetTime()
        {

            string[] _kytudau = new string[60];
            for (int i = 0; i < 15; i++)
            {
                _kytudau[(i * 3)] = "GioLamViec" + Convert.ToString(i + 1) + " = ";
                _kytudau[(i * 3) + 1] = "GioNghi" + Convert.ToString(i + 1) + " = ";
                _kytudau[(i * 3) + 2] = "KetThuc" + Convert.ToString(i + 1) + " = ";
            }

            string[] _thoigian = new string[50];

            _thoigian[0] = "SoGioLam = " + cbSoGioLam.Text.Trim();
            _thoigian[1] = "GioBatDau = " + mtxtGioBatDau.Text;
            if (v_IDTo == 1 || v_IDTo == 7)
            {
                _thoigian[2] = "MatrixInterval = " + txtTGDelay.Text.Trim();
            }
            else
            {
                _thoigian[2] = "MatrixInterval = " + v_TimeDelay;
            }

            for (int i = 0; i < 15; i++)
            {
                _thoigian[(i * 3) + 3] = _kytudau[(i * 3)] + v_ThoiGianLam[i].Text;
                _thoigian[(i * 3) + 4] = _kytudau[(i * 3) + 1] + v_ThoiGianNghi[i].Text;
                _thoigian[(i * 3) + 5] = _kytudau[(i * 3) + 2] + v_GioKetThuc[i].Text;
            }


            return _thoigian;

        }
        private string[] GetTimeNew()
        {
            string[] _thoigian = new string[50];

            _thoigian[0] = "" + cbSoGioLam.Text.Trim();
            _thoigian[1] = "" + mtxtGioBatDau.Text;
            if (v_IDTo == 1 || v_IDTo == 7)
            {
                _thoigian[2] = "" + txtTGDelay.Text.Trim();
            }
            else
            {
                _thoigian[2] = "" + v_TimeDelay;
            }

            for (int i = 0; i < 15; i++)
            {
                _thoigian[(i * 3) + 3] = v_ThoiGianLam[i].Text;
                _thoigian[(i * 3) + 4] = v_ThoiGianNghi[i].Text;
                _thoigian[(i * 3) + 5] = v_GioKetThuc[i].Text;
            }


            return _thoigian;

        }
        // tinh thoi gian ket thuc cho tung control txtGioKetThuc sua ngay 06/06/2008
        private void TinhThoiGianKetThuc()
        {
            char[] equalsign = new char[1];
            equalsign[0] = ':';
            string _tam = this.mtxtGioBatDau.Text;
            string[] _GioPhut = _tam.Split(equalsign);
            if (_GioPhut[0] == "")
            {
                return;
            }
            v_gioBatDau = Convert.ToInt32(_GioPhut[0]);
            if(_GioPhut[1]=="")
                v_phutBatDau = Convert.ToInt32(0);
            else
                v_phutBatDau = Convert.ToInt32(_GioPhut[1]);
            string _tamGio = "";
            string _tamPhut = "";
            for (int i = 0; i < 20; i++)
            {
                try
                {
                    if (Convert.ToInt32(v_ThoiGianLam[i].Text) != 0 || Convert.ToInt32(v_ThoiGianNghi[i].Text) != 0)
                    {
                        this.TinhGio(Convert.ToInt32(v_ThoiGianLam[i].Text), Convert.ToInt32(v_ThoiGianNghi[i].Text));
                        _tamGio = v_gioBatDau.ToString();
                        if (_tamGio.Length == 1)
                        {
                            _tamGio = "0" + _tamGio;
                        }
                        _tamPhut = v_phutBatDau.ToString();
                        if (_tamPhut.Length == 1)
                        {
                            _tamPhut = "0" + _tamPhut;
                        }
                        v_GioKetThuc[i].Text = _tamGio + ":" + _tamPhut + ":00";
                    }
                    else
                    {
                        v_GioKetThuc[i].Text = "00:00:00";
                    }
                }
                catch
                {
                    return;
                }
            }
        }
        private void TinhGio(int _phut1, int _phut2)
        {
            v_phutBatDau += _phut1 + _phut2;
            if (v_phutBatDau > 59)
            {
                v_phutBatDau -= 60;
                v_gioBatDau += 1;
                if (v_phutBatDau > 59)
                {
                    v_phutBatDau -= 60;
                    v_gioBatDau += 1;
                }
            }
        }
        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            try
            {
               // SaveTimeOfWeek(ScadaClient.Classes.frmMain.__configDir, v_IDTo);
                String sErr = "";
                string[] _TG = this.GetTimeNew();
              //  Classes.frmMain.__config.Timer.Rows[0]["ID"] = "-1";
              //  Classes.frmMain.__config.Timer.Rows[0]["Data"] = string.Join("\n", _TG);
               // Classes.frmMain.SaveConfig();
                if (v_IDTo != 0)
                {
                    DTO.OpGioInfo info = GetValue();
                    BUS.OpGioControl ctr = new BUS.OpGioControl(_pecController.Bus);
                    sErr = ctr.InsertUpdate(info);
                }
                else
                {
                    sErr = SaveAllChuyen();
                }
                if(sErr == "")
                    MessageBox.Show("Lưu thời gian làm việc đã hoàn tất", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show(sErr, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch
            {
                MessageBox.Show("Lưu thời gian làm việc có lỗi.vui lòng thực hiện lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string SaveAllChuyen()
        {            
            string sErr = "";
            DTO.ChuyenSanXuatInfo info = new DTO.ChuyenSanXuatInfo();
            BUS.ChuyenSanXuatControl ctr = new BUS.ChuyenSanXuatControl(_pecController.Bus);
            DataTable dt = ctr.GetByFilterToDataTable(_pecController.XuongId, null, ref sErr);
            DTO.OpGioInfo infoGio = GetValue();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                info = new DTO.ChuyenSanXuatInfo(dt.Rows[i]);
                infoGio.LineId = info.LineId;
                BUS.OpGioControl ctrGio = new BUS.OpGioControl(_pecController.Bus);
                sErr += ctrGio.InsertUpdate(infoGio);
            }
            return sErr;
        }

        
        
        private void SaveTimeOfWeek(string sPath, int iLine)
        {
            int iMonth = DateTime.Today.Month;
            int timeInMonth = GetTimeOfMonth();

            List<string> thoiGianLamViec = new List<string>();
            //clsIOStreams iostream = new clsIOStreams();
         //   string[] thoigianLine = Classes.frmMain.__config.Calendar.Rows[0]["Data"].ToString().Split('\n');
            //for (int line = 0; line < thoigianLine.Length; line++)
            //{
            //    //string tmpex = iostream.Read_OneRow(sPath + ScadaClient.Classes.frmMain.__calendarFile, line);
            //    thoiGianLamViec.Add(thoigianLine[line]);
            //}
            for (int i = 1; i < thoiGianLamViec.Count; i++)
            {
                if (iLine == 0 || i / 13 == (iLine - 1))
                {
                    string[] tmp = thoiGianLamViec[i].Split(',');
                    if (tmp.Length < 28 && iMonth == i % 13)
                    {
                        tmp = new string[DateTime.DaysInMonth(DateTime.Today.Year, iMonth) + 1];
                        tmp[0] = ToMonthString(iMonth);
                    }
                    if (tmp.Length > 1)
                    {
                        for (int j = 1; j <= tmp.Length; j++)
                        {
                            try
                            {
                                DateTime day = new DateTime(DateTime.Now.Year, i % 13, j);
                                if (day.DayOfWeek != DayOfWeek.Sunday)
                                    tmp[j] = timeInMonth.ToString();
                                else
                                    tmp[j] = "0";
                            }
                            catch { }

                        }
                        thoiGianLamViec[i] = string.Join(",", tmp);

                    }
                }
            }
          //  Classes.frmMain.__config.Calendar.Rows[0]["Data"] = string.Join("\n", thoiGianLamViec.ToArray());

          //  Classes.frmMain.SaveConfig();
            //iostream.SaveFileText(thoiGianLamViec, filename);
        }

        private string ToMonthString(int iMonth)
        {
            switch (iMonth)
            {
                case 1: return "Mon";
                case 2: return "Feb";
                case 3: return "Mar";
                case 4: return "Apr";
                case 5: return "May";
                case 6: return "Jun";
                case 7: return "Jul";
                case 8: return "Aug";
                case 9: return "Sep";
                case 10: return "Oct";
                case 11: return "Nov";
                case 12: return "Dec";
            }
            return "";
        }
        private void cbSoGioLam_SelectedValueChanged(object sender, EventArgs e)
        {
            //  tinh so phut lam việc
            int _SoLuong = Convert.ToInt32(this.cbSoGioLam.Text.Trim());

            for (int i = 0; i < _SoLuong; i++)
            {
                gb[i].Visible = true;
            }
            for (int i = 19; i >= _SoLuong; i--)
            {
                gb[i].Visible = false;
            }
            v_TrangThai = 0;
            for (int j = 0; j < 20; j++)
            {
                if (gb[j].Visible == false)
                {
                    v_ThoiGianLam[j].Text = "0";
                    v_ThoiGianNghi[j].Text = "0";
                    v_GioKetThuc[j].Text = "00:00:00";
                }
            }
            v_TrangThai = 1;
        }

        private void mtxtGioBatDau_Validated(object sender, EventArgs e)
        {
            char[] equalsign = new char[1];
            equalsign[0] = ':';
            string _tam = this.mtxtGioBatDau.Text;
            string[] _GioPhut = _tam.Split(equalsign);
            v_gioBatDau = Convert.ToInt32(_GioPhut[0]);
            if(_GioPhut[1]=="")
                v_phutBatDau = Convert.ToInt32(0);
            else
                v_phutBatDau = Convert.ToInt32(_GioPhut[1]);
        }
        private void txtNgayHienTai_Validated(object sender, EventArgs e)
        {
            v_NgayHienTai = this.txtNgayHienTai.Text;
        }


        private void txtGioLamThu1_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu1_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu2_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu2_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu3_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu3_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu4_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu4_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu5_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu5_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu6_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu6_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu7_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu7_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu8_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu8_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu9_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu9_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu10_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu10_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu11_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu11_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu12_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu12_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu13_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu13_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu14_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu14_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu15_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu15_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu16_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu16_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu17_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu17_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu18_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu18_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu19_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu19_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }

        private void txtGioLamThu20_Validated(object sender, EventArgs e)
        {
            if (v_TrangThai == 0)
            {
                return;
            }
            this.TinhThoiGianKetThuc();
        }
        private void txtGioNghiThu20_Validated(object sender, EventArgs e)
        {
            v_TrangThai = 1;
            this.TinhThoiGianKetThuc();
        }
    }
}